package top.chukongxiang.monitorservice.javafx.core;

import javafx.scene.Node;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/19 13:34
 */
public class FxContextHolder {

    private static final Map<String, Stage> STAGE_CACHE = new HashMap<>();
    private static final Map<String, Map<String, Object>> NAMESPACE_CACHE = new HashMap<>();

    public static void set(String key, Stage stage) {
        STAGE_CACHE.put(key, stage);
    }

    public static void set(String key, Map<String, Object> namespace) {
        NAMESPACE_CACHE.put(key, namespace);
    }

    public static void set(String key, Stage stage, Map<String, Object> namespace) {
        set(key, stage);
        set(key, namespace);
    }

    public static Stage get(String key) {
        return STAGE_CACHE.get(key);
    }

    public static <T extends Node> T getNode(String key, String fxId) {
        Map<String, Object> namespace = NAMESPACE_CACHE.get(key);
        return namespace == null ? null : (T) namespace.get(fxId);
    }

    public static ToggleGroup getToggleGroup(String key, String fxId) {
        Map<String, Object> namespace = NAMESPACE_CACHE.get(key);
        if (namespace == null) {
            return null;
        }

        return namespace == null ? null : (ToggleGroup) namespace.get(fxId);
    }
}
